//
//  MoreViewController.m
//  A_Comic
//
//  Created by sos on 16/1/20.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "MoreViewController.h"
#import <AFNetworking.h>
#import "MoreTableViewCell.h"
#import "HomePageModel.h"
#import <UIImageView+WebCache.h>
#import "DetailsViewController.h"
@interface MoreViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain) UITableView *tableView;//整页tableview
@property (nonatomic,retain) NSMutableArray *dataArr;//保存数据的数组
@end

@implementation MoreViewController
- (void)dealloc
{
    [_dataArr release];
    [_tableView release];
    [_str release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    // Do any additional setup after loading the view.
    
    //整页tableview的创建
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    //tableView的注册
    [self.tableView registerClass:[MoreTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self data];
    
}

-(void)data
{
    static NSString *url;
    if (self.str.length <=3) {
        
        url = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Classify/comicClassifyDetailForIos/uin/1055701255/local_version/3.6.1/channel/1001/guest_id/FBB11919-34E9-4168-9491-811B4C0BDBD6/classify_id/%@/page/1/listcnt/20",self.str];
    }else{
        
        url = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Classify/categoryList/uin/0/local_version/3.5.1/channel/1001/guest_id/FBB11919-34E9-4168-9491-811B4C0BDBD6/category_id/%@/page/1/listcnt/20",self.str];
    }
    
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr = responseObject[@"data"];
        self.dataArr = [HomePageModel modelHandlerWithArray:arr];
        [self.tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
    
}

// tableView的协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    HomePageModel *m = self.dataArr[indexPath.row];
    [cell.img sd_setImageWithURL:[NSURL URLWithString:m.cover_url]];
    cell.nameLabel.text = m.title;
    cell.label.text = [NSString stringWithFormat:@"作者:%@",m.artist_name];
    cell.lastLabel.text = [NSString stringWithFormat:@"更新至%@",m.lastup];
    return cell;
}
//cell高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 120;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsViewController *deta = [[DetailsViewController alloc]init];
    deta.m = self.dataArr[indexPath.row];
    [self.navigationController pushViewController:deta animated:YES];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
